package com.zkjl.protect_business.utils;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ReUtil;
import com.zkjl.protect_business.common.BaseException;
import com.zkjl.protect_business.entity.DocField;
import com.zkjl.protect_business.vo.ParamVO;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.zkjl.protect_business.common.Status.BAD_REQUEST;

/**
 * 常用工具类
 */
public class CommonUtil {

    /**
     * 提取身份证号
     * @param content
     * @return
     */
    public static String getIdCard(String content){

        String regex = "\\d{17}[\\\\d|x]|\\d{15}";
        String result = ReUtil.get(regex, content, 0);

        return result;
    }

    /**
     * 替换特殊字符
     * @param str
     * @return
     */
    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }


        /**
         * 获取文件名称里面唯一标识
         * @param str
         * @return
         */
    public static ParamVO getParam(String str) {

        if (StringUtils.isBlank(str)) {
            throw new BaseException(BAD_REQUEST.getCode(), "文件名不能为空！");
        }
        int i = str.lastIndexOf(".");
        if (i == -1) {
            throw new BaseException(BAD_REQUEST.getCode(), "文件必须有后缀名！");
        }

        int i1 = str.lastIndexOf("-");
        if (i1 == -1) {
            throw new BaseException(BAD_REQUEST.getCode(), "文件名必须以 - 分隔唯一标识 张三-手机号:13888888888.jpge  手机号=13888888888为唯一标识");
        }

        String substring = str.substring(i1 + 1, i);

        boolean contains = substring.contains("=");
        if (!contains) {
            throw new BaseException(BAD_REQUEST.getCode(), "文件名中的唯一标识必须以 = 分隔,类似 张三-手机号=13888888888.jpge");
        }
        String[] split = substring.split("=");
        if (split.length != 2) {
            throw new BaseException(BAD_REQUEST.getCode(), "文件名中的唯一标识分隔符 = 只能有一个");
        }

        ParamVO paramVO = new ParamVO();
        paramVO.setKey(split[0]);
        paramVO.setValue(split[1]);

        return paramVO;

    }




}
